<html>
<head><meta charset="utf-8"><title>Visiting assigment to binary operator · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Visiting.20assigment.20to.20binary.20operator.html">Visiting assigment to binary operator</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="217383843"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Visiting%20assigment%20to%20binary%20operator/near/217383843" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Robin Moussu <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Visiting.20assigment.20to.20binary.20operator.html#217383843">(Nov 20 2020 at 10:28)</a>:</h4>
<p>I'm doing an analysis that uses <code>mir::visiit::Visitor::visit_assign</code>. When the <code>rvalue</code> is a binary operator, can I assume that the left operator is the same than the <code>lvalue</code> of the assignment?</p>
<p>documentation: <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/mir/enum.Rvalue.html"><code>Rvalue</code></a> and <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/mir/visit/trait.Visitor.html#method.visit_assign">`visit_assign</a></p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">pub</span><span class="w"> </span><span class="k">enum</span> <span class="nc">Rvalue</span><span class="o">&lt;'</span><span class="na">tcx</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">BinaryOp</span><span class="p">(</span><span class="n">BinOp</span><span class="p">,</span><span class="w"> </span><span class="n">Operand</span><span class="o">&lt;'</span><span class="na">tcx</span><span class="o">&gt;</span><span class="p">,</span><span class="w"> </span><span class="n">Operand</span><span class="o">&lt;'</span><span class="na">tcx</span><span class="o">&gt;</span><span class="p">),</span><span class="w"></span>
<span class="w">    </span><span class="o">..</span><span class="p">.</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
<span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">visit_assign</span><span class="p">(</span><span class="w"></span>
<span class="w">    </span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="bp">self</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">lvalue</span>: <span class="kp">&amp;</span><span class="nc">Place</span><span class="o">&lt;'</span><span class="na">tcx</span><span class="o">&gt;</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">rvalue</span>: <span class="kp">&amp;</span><span class="nc">Rvalue</span><span class="o">&lt;'</span><span class="na">tcx</span><span class="o">&gt;</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">location</span>: <span class="nc">Location</span><span class="w"></span>
<span class="p">)</span><span class="w"></span>
</code></pre></div>



<a name="217528440"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Visiting%20assigment%20to%20binary%20operator/near/217528440" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Visiting.20assigment.20to.20binary.20operator.html#217528440">(Nov 22 2020 at 01:33)</a>:</h4>
<p>Hmm, why would you be able to assume that? Perhaps I'm misunderstanding, but this MIR:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="n">_3</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">_1</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">_2</span><span class="p">;</span><span class="w"></span>
</code></pre></div>
<p>would seem to not satisfy the condition of the left operand (<code>_1</code>) being the same as the lvalue (<code>_3</code>). Or if you mean "can I assume that the left operand is <em>different</em> from the lvalue", I think that also wouldn't hold since <code>Place</code>s can be mutable and thus this should be valid MIR:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">_3</span>: <span class="kt">i32</span><span class="p">;</span><span class="w"></span>
<span class="c1">// ...</span>
<span class="n">_3</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"></span>
<span class="n">_3</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">_3</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span><span class="w"></span>
</code></pre></div>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>